package com.dwm716.shorturl.service.impl;

import com.dwm716.shorturl.pagehelper.PageQuery;
import com.github.pagehelper.PageHelper;
import com.dwm716.shorturl.common.ApiResponse;
import com.dwm716.shorturl.domain.RequestRecord;
import com.dwm716.shorturl.mapper.RequestRecordMapper;
import com.dwm716.shorturl.pagehelper.PageResult;
import com.dwm716.shorturl.service.RequestRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author biliyu
 * @date 2024/8/1 14:02
 */

@Service
public class RequestRecordServiceImpl implements RequestRecordService {

    @Autowired
    private RequestRecordMapper requestRecordMapper;

    @Override
    public int insert(RequestRecord requestRecord) {
        return requestRecordMapper.insert(requestRecord);
    }

    @Override
    public ApiResponse<PageResult<RequestRecord>> getRequestRecordList(PageQuery query) {
        com.github.pagehelper.Page<Object> page = PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<RequestRecord> list = requestRecordMapper.getRequestRecordList(query);
        PageResult<RequestRecord> requestRecordPageResult = PageResult.create(list, page.getTotal(), page.getPageSize());
        return ApiResponse.ok(requestRecordPageResult);
    }

    @Override
    public Integer countByUrlId(Integer id) {
        Example example = new Example(RequestRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("urlId", id);
        return requestRecordMapper.selectCountByExample(example);
    }
}
